var G_PATH = "http://care.wisedu.com/";

// 验证登录信息
function getUserData(self, user, password, url) {
	$.ajax({
		type: "POST",
		dataType: "json",
		url: G_PATH + url,
		data: {
			account: user,
			password: password
		},
		success: function(data) {
			var role = data.data.roleId;

			function routerJump(name) {
				self.$router.go({
					name: name,
					params: {
						deptId: "00001"
					}
				})
			}
			switch (role) {
				case 1:
					routerJump("overView");
					break;
				case 2:
					routerJump("infomationCenterOverView");
					break;
				case 4:
					routerJump("adminOverView");
					break;
				case 7:
					routerJump("applicationOverView");
					break;
				case 8:
					routerJump("overView");
					break;
			}
		},
		error: function() {
			alert("账号或密码错误！！！")
		}
	})
}

//横向柱状图标组件数据请求
function getLateralBarCharts(time, url, kind) {
	var _self = this;
	var axisname = [];
	var average = '';
	var colorObj = {
		'floor': '#ffcd00',
		'ceil': '#20d4cd',
		'equal': '#56d5ff'
	};
	$.ajax({
		type: "POST",
		url: G_PATH + url,
		data: {
			querytype: time,
			deptId: kind
		},
		dataType: "json",
		success: function(data) {
			var chartsData = data.data.charts;
			for (var i = 0; i < chartsData.length; i++) {
				var averageNum = parseInt(chartsData.length / 2);
				chartsData[i].itemStyle = {
					normal: {
						color: (chartsData[i].value > chartsData[averageNum].value) ? colorObj['floor'] : colorObj['ceil']
					}
				}
				axisname.push(chartsData[i].name);
			}
			_self.showLoading({
				text: '暂无数据'
			});
			if (chartsData.length != '0') {
				_self.hideLoading()
			};
			_self.setOption({
				yAxis: {
					data: axisname
				},
				series: [{
					data: chartsData,
					label: {
						normal: {
							show: true,
							position: 'right',
							formatter: function(params) {
								return (params.data.desc)
							}
						}
					},
					markLine: {
						symbol: '',
						label: {
							normal: {
								show: true,
								formatter: '{b}:{c}',
								position: 'start'
							}
						},
						lineStyle: {
							normal: {
								type: 'dashed',
								color: '#aaeaff'
							}
						},
						data: [{
							type: 'average',
							name: '平均值'
						}]
					}
				}]
			});
		}
	});
}

// 饼图组件数据请求
function getPieCharts(time, url) {
	var _self = this;
	var colorObj = ['#03afe8', '#38c0ed', '#68cff1', '#62d6d2', '#81dedb', '#a1e6e4', '#66c41a', '#85d048', '#b6e391', '#ffcd00', '#ffd733', '#ffe166', '#ffeb99'];
	$.ajax({
		type: "POST",
		url: G_PATH + url,
		dataType: "json",
		data: {
			querytype: time
		},
		success: function(data) {
			var axisname = [];
			var chartsData = data.data.charts;
			var totle = data.data.sum;
			for (var i = 0; i < chartsData.length; i++) {
				chartsData[i].itemStyle = {
					normal: {
						color: colorObj[i]
					}
				};
				chartsData[i].name = chartsData[i].name
			}
			chartsData[2].selected = true;
			_self.setOption({
				series: [{
					data: chartsData,
					label: {
						normal: {
							show: true,
							formatter: function(params, ticket, callback) {
								if (params.data.selected) {
									return ""
								} else {
									return '总应用数\n' + totle + '个'
								}
							},
							position: 'center',
							textStyle: {
								fontSize: '20',
								fontWeight: 'normal',
								color: '#68cff1'
							}
						},
						emphasis: {
							show: false
						}
					}
				}]
			});
		}
	})
}

// 统计部门筛选按钮
function getDeptmentFilter(self, url) {
	$.ajax({
		url: G_PATH + url,
		dataType: "json",
		success: function(data) {
			data.data.unshift({
				"dept_id": "all",
				"dept_name": "所有部门"
			});
			self.departmentList = data.data;
		}
	});
}

// 数据概览

function getTotleView(url, time, kind) {
	var self = this;
	$.ajax({
		type: "GET",
		url: G_PATH + url,
		data: {
			querytype: time,
			deptId: kind
		},
		dataType: "json",
		success: function(data) {
			var dataAverage = data.data;
			if (data.data.average < 3600) {
				var min = parseInt(data.data.average % 3600 / 60) + "m";
				dataAverage['average'] = min;
			} else {
				var hour = parseInt(data.data.average / 3600) + "h";
				var min = parseInt(data.data.average % 3600 / 60) + "m";
				var average = hour + min;
				dataAverage['average'] = average;
			}
			self.processdata = dataAverage
		}
	});
}

//rank组件排名列表数据渲染
function getRankList(url, kind, time, efft) {
	var self = this;
	$.ajax({
		type: "GET",
		url: G_PATH + url,
		data: {
			deptId: kind,
			querytype: time,
			sequence: efft
		},
		dataType: "json",
		success: function(data) {
			self.rankList = data.data;
		},
		error: function() {
			console.log("数据获取失败！！");
		}
	});
}

// 应用中心数据概览图
function getAppCenterView(url, time, kind) {
	var self = this;
	$.ajax({
		type: "GET",
		url: G_PATH + url,
		data: {
			querytype: time,
			deptId: kind
		},
		dataType: "json",
		success: function(data) {
			self.processdata = data.data;
		}
	});
}

// 应用中心数据概览柱状图
function getAppCenterBar(time, url, kind) {
	var _self = this;
	var colorObj = {
		'floor': '#ffcd00',
		'ceil': '#20d4cd',
		'equal': '#56d5ff'
	};
	$.ajax({
		type: 'GET',
		data: {
			querytype: time,
			deptId: kind
		},
		url: G_PATH + url,
		dataType: 'json',
		success: function(data) {
			var barChartsData = data.data.totalCharts;
			var axisname = [];
			for (var i = 0; i < barChartsData.length; i++) {
				barChartsData[i].itemStyle = {
					normal: {
						color: (barChartsData[i].value > 60) ? colorObj['floor'] : colorObj['ceil']
					}
				}
				axisname.push(barChartsData[i].name);

			}
			_self.setOption({
				xAxis: {
					data: axisname
				},
				series: [{
					data: barChartsData
				}]
			});
		},
		error: function() {
			console.log("获取数据失败！！！")
		}
	})
}

// 应用中心卡片
function getCardData(time, url, kind) {
	var self = this;
	var length = '';
	$.ajax({
		type: "GET",
		url: G_PATH + url,
		dataType: "json",
		data: {
			querytype: time,
			deptId: kind
		},
		success: function(data) {
			self.departmentCenterCard = data.data;
			length = data.data.length;
			if (length % 2 == 0) {
				self.ismore = true;
			};
		},
		error: function() {
			console.log('数据获取失败！！');
		}
	});
}

// 健康度数据概览
function getHealthData(url, time) {
	var self = this;
	$.ajax({
		type: "GET",
		dataType: "json",
		url: G_PATH + url,
		data: {
			time: time
		},
		success: function(data) {
			self.data = data;
		},
		error: function() {
			console.log("获取数据失败！！！")
		}
	})
}

// 部门数据概览
function getDeptmentOverview(time, url, kind) {
	var self = this;
	$.ajax({
		type: 'GET',
		url: G_PATH + url,
		dataType: "json",
		data: {
			querytype: time,
			deptId: kind
		},
		success: function(data) {
			self.application = data.data
		},
		error: function() {
			console.log('数据获取失败！！');
		}
	});
}

// 监控中心概览
function getCardList(self, url) {
	$.ajax({
		type: 'GET',
		url: G_PATH + url,
		dataType: "json",
		success: function(data) {
			self.cardList = data.data
		},
		error: function() {
			console.log('数据获取失败！！');
		}
	});
}

module.exports = {
	getUserData: getUserData,
	getLateralBarCharts: getLateralBarCharts,
	getPieCharts: getPieCharts,
	getDeptmentFilter: getDeptmentFilter,
	getTotleView: getTotleView,
	getRankList: getRankList,
	getAppCenterView: getAppCenterView,
	getAppCenterBar: getAppCenterBar,
	getCardData: getCardData,
	getHealthData: getHealthData,
	getDeptmentOverview: getDeptmentOverview,
	getCardList: getCardList
}